var setNicknameResult = true;

/**
 * 设置初始化
 */
function setupInit() {

	var sexback = $("#sexback").val();
	var bloodTypeback = $("#bloodTypeback").val();
	var incomeback = $("#incomeback").val();
	var experienceback = $("#experienceback").val();

	$("input[type='radio'][name='sex'][value='" + sexback + "']").attr(
			"checked", "checked");
	if (bloodTypeback == "") {
		$("#bloodType").val("O");
	} else {
		$("#bloodType").val(bloodTypeback);
	}

	if (incomeback == "") {
		$("#income").val("0");
	} else {
		$("#income").val(incomeback);
	}

	if (experienceback == "") {
		$("#experience").val("");
	} else {
		$("#experience").val(experienceback);
	}

}

function checkPassword(password) {

	var passwordshow = $("#passwordshow");
	$
			.ajax({
				url : base_domain + '/user/checkPassword.html',
				data : "password=" + hex_md5(password),
				type : 'post',
				dataType : 'json',
				contentType : "application/x-www-form-urlencoded;charset=utf-8",
				success : function(result) {
					var html = '';
					if (result) {
						html = "<img src='../images/register/true.png' class='r-true'>";
						$("#updatepass").attr("disabled", false);
					} else {
						html = "密码不正确!";
					}
					passwordshow.html(html);
				},

				error : function() {
					setNicknameResult = false;
					alert("连接服务器出错");
				}
			});
}

$("#updatepass").click(function() {
	var password = $("#passwordRegister").val();
	$.ajax({
		url : base_domain + '/user/modifyPassword.html',
		data : "password=" + hex_md5(password),
		type : 'post',
		dataType : 'json',
		contentType : "application/x-www-form-urlencoded;charset=utf-8",
		success : function() {
			alert("修改成功!");
			self.location = "../user/setup.html";
		},

		error : function() {
			setNicknameResult = false;
			alert("连接服务器出错");
		}
	});
});
/**
 * 更新并保存
 */

$(".setup-save")
		.click(
				function() {
					var newSex = $('input[type="radio"][name="sex"]:checked')
							.val();
					var newBloodtype = $("#bloodType").val();
					var newIncome = $("#income").val();
					var newExperience = $("#experience").val();

					if (setNicknameResult) {
						$
								.ajax({
									url : base_domain + '/user/setup.html',
									data : "sex=" + newSex + "&bloodType="
											+ newBloodtype + "&income="
											+ newIncome + "&experience="
											+ newExperience,
									type : 'post',
									dataType : 'json',
									contentType : "application/x-www-form-urlencoded;charset=utf-8",
									success : function(result) {
										if (result == true) {
											alert("更新成功");
										} else {
											alert("更新失败");
										}
									},

									error : function() {
										setNicknameResult = false;
										alert("连接服务器出错");
									}
								});
					}

				});

/**
 * 头像上传时验证
 */
function checkPortrait() {
	var portraitfile = $(".portrait-upload").val();

	if (portraitfile == undefined || portraitfile == "" || portraitfile == null) {
		alert("请选择要上传的图片");
		return false;
	}

	var index = portraitfile.lastIndexOf("\\");
	var name = portraitfile.substr(index + 1, portraitfile.length);
	index = name.lastIndexOf(".");
	var suffix = name.substr(index + 1, name.length).toLowerCase();

	if (suffix == "jpg" || suffix == "jpeg" || suffix == "png"
			|| suffix == "bmp") {
		return true;
	}

	alert("只能上传jpg、jpeg、png、bmp格式的图片");
	return false;
}


/**
 * 证件上传时验证
 */
function checkLicense() {
	var licensefile = $(".license-upload").val();

	if (licensefile == undefined || licensefile == "" || licensefile == null) {
		alert("请选择要上传的执照图片");
		return false;
	}

	var index = licensefile.lastIndexOf("\\");
	var name = licensefile.substr(index + 1, licensefile.length);
	index = name.lastIndexOf(".");
	var suffix = name.substr(index + 1, name.length).toLowerCase();

	if (suffix == "jpg" || suffix == "jpeg" || suffix == "png"
			|| suffix == "bmp") {
		return true;
	}

	alert("只能上传jpg、jpeg、png、bmp格式的图片");
	return false;
}

/**
 * 检查手机号码是否符合要求<br>
 * 手机号码验证，验证13系列和150-159(154除外)、180、185、186、187、188、189几种号码，长度11位
 * 
 * @param val
 */
function checkMobile(val) {
	value = val.trim();
	if (/^13\d{9}$/g.test(value) || (/^15[0-35-9]\d{8}$/g.test(value))
			|| (/^18[05-9]\d{8}$/g.test(value))) {
		$("#mobileshow").html("");
		$("#sendCode").attr("disabled", false);
	} else {
		$("#mobileshow").html("手机号码不合法");
		$("#sendCode").attr("disabled", true);
	}
}

/**
 * 发送验证码
 */
$("#sendCode").click(function() {
	$("#sendCode").attr("disabled", true);
	$("#mobile").attr("disabled", true);
	// 每隔1s刷新一次
	var clock = 120;//120s后再次发送

	var clocker = setInterval(function() {
		$("#sendCode").val("重新发送"+(clock--)+"s");
		if (clock == -1) {
			clearInterval(clocker);
			$("#sendCode").val("再次发送");
			$("#mobileshow").html("");
			$("#sendCode").attr("disabled", false);
			$("#mobile").attr("disabled", false);
		}
	}, 1000);

	$.ajax({
		url : base_domain + '/user/sendMobileCode.html',
		data : "mobile=" + $("#mobile").val().trim(),
		type : 'post',
		dataType : 'json',
		contentType : "application/x-www-form-urlencoded;charset=utf-8",
		success : function(result) {
			if (result > 0) {
				$("#mobileshow").html("发送成功，请查收");
			} else {
				$("#mobileshow").html("发送失败");
			}
		},
		error : function() {
			alert("连接服务器出错");
		}
	});

});


/**
 * 手机验证码 6位
 */
function checkMobileCode(val) {
	if (val == null || val == "") {
		return;
	}
	var code = parseInt(val);
	if (code != null && 0 <= code && code <= 999999) {
		$("#bindMobile").attr("disabled", false);
	} else {
		$("#bindMobile").attr("disabled", true);
	}

}

/**
 * 验证 验证码
 */
$("#bindMobile").click(function() {
	
	$.ajax({
		url : base_domain + '/user/verifyMobileCode.html',
		data : "mobileCode=" + $("#mobileCode").val().trim(),
		type : 'post',
		dataType : 'json',
		contentType : "application/x-www-form-urlencoded;charset=utf-8",
		success : function(result) {
			if (result == true) {
				$("#mobileCodeShow").html("绑定成功");
				$("#bindMobile").attr("disabled", true);
				alert("绑定成功");
				history.go(0);
			} else {
				$("#mobileCodeShow").html("绑定失败");
			}
		},
		error : function() {
			alert("连接服务器出错");
		}
	});
});
